<?php
error_reporting(E_ALL & ~E_NOTICE);
ob_start();
//============ Start Session และทำการเรียก Function ติดต่อฐานข้อมูล 
session_start();
include("../connect/connect.php");

//เมื่อเข้าหน้าบิลจะเปลี่ยนสถานะเป็นเช็คบิลแล้ว
mysql_query("UPDATE table_status SET table_status_group = '1' WHERE table_status_table = '".$_SESSION["table"]."' ");

	//สร้างเลขบิล
	$NoQuery = mysql_query("SELECT max(bill_id) as bill_id from bill");
	$BillRow = mysql_fetch_array($NoQuery);
	
	$BillQuery = mysql_query("SELECT bill_no1,bill_no2 FROM bill WHERE bill_id = '".$BillRow['bill_id']."' ");
	$BillNum = mysql_fetch_array($BillQuery);
	
	$BillNumId = $BillRow['bill_id']+1;
	$year = date("Y")+543;
	$month = date("m");
	$time = time();
	$d = date("H", $time);

	if($d<='5'){
		$ddate = strtotime("-1 day" , $time);
		$day = date("Ymd",  $ddate);
	} else {
		$day = date("Ymd", $time);
	}
	
	$bill_Num = $day;
	
	if($BillNum['bill_no1']==$bill_Num){
		$BillNumber = $BillNum['bill_no2']+1;
		$resultNum=sprintf("%04d", $BillNumber);
		$billNo = $bill_Num.$resultNum;
		mysql_query("INSERT INTO bill (bill_no1,bill_no2,bill_no) VALUES ('".$bill_Num."','".$BillNumber."','".$billNo."')");
		mysql_query("UPDATE table_status SET bill_id = '".$BillNumId."' WHERE table_status_table = '".$_SESSION["table"]."' ");
	} else {
		$BillNumber = '1';
		$resultNum=sprintf("%04d", $BillNumber);
		$billNo = $bill_Num.$resultNum;
		mysql_query("INSERT INTO bill (bill_no1,bill_no2,bill_no) VALUES ('".$bill_Num."','".$BillNumber."','".$billNo."')");
		mysql_query("UPDATE table_status SET bill_id = '".$BillNumId."' WHERE table_status_table = '".$_SESSION["table"]."' ");
	}
	
	/*
	$BillNo = $BillRow['bill_id']+1;
	$result=sprintf("%05d", $BillNo);
	$month = date("m");
	$year = date("Y")+543;
	$day = date("d");
	$bill = $year.$month.$day.$result;
	$bill_Num = $year.$month.$day;
	
	$ChBill = mysql_query("SELECT bill_no1 FROM bill WHERE bill_no2 = '".$BillRow['bill_no2']."' ");
	$ChBillRow = mysql_fetch_array($ChBill);
	
	echo $BillNo;
	
	if($bill_Num==$ChBillRow['bill_no1']){
		echo $BillNo;
	} else {
		echo '1';
	}
	*/
	//echo $ChBillRow['bill_no1'];
//	mysql_query("INSERT INTO bill (bill_id,bill_no) VALUES ('".$BillNo."','".$bill."')");
	
	
if($_SESSION['user_username']=="" && $_SESSION["user_name"]=="" && $_SESSION["user_surname"]==""){
	echo "<script>window.location = '../index.php'</script>";
}

function utf8_substr($str,$start_p,$len_p) 
{ 
   preg_match_all("/./u", $str, $ar); 

   if(func_num_args() >= 3) { 
       $end = func_get_arg(2); 
       return join("",array_slice($ar[0],$start_p,$len_p)); 
   } else { 
       return join("",array_slice($ar[0],$start_p)); 
   } 
} 

function substr_utf8( $str, $start_p , $len_p){
 return preg_replace( '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start_p.'}'.
  '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len_p.'}).*#s',
  '$1' , $str );
};

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>-</title>
<link rel="stylesheet" href="stylebill.css" type="text/css" />
<link rel="shortcut icon" href="../image/Letter-T.ico" />
</head>
<?php
/*
	$NoQuery = mysql_query("SELECT bill_no FROM bill,table_status WHERE table_status.bill_id = bill.bill_id AND table_status_table = '".$_SESSION["table"]."'");
	$BillRow = mysql_fetch_array($NoQuery);
	$bill = $BillRow['bill_no'];
*/
?>
<body onload="window.print();">
<table class="mainbill" border="0" cellpadding="2" cellspacing="1" bgcolor="#666666" align="left">
  <tr>
    <td width="90%" valign="top" bgcolor="#FFFFFF">
	<div align="left" class="headbill"><strong>เลขที่ <?php echo $billNo; ?></strong></div>
      <p><strong><center>ใบแจ้งรายการค่าใช้จ่ายอาหารและเครื่องดื่ม</center></strong></p>

<div align="right" class="headbill"><strong>วันที่</strong> :  <?php $y=date("Y")+543; echo date("d/m/").$y; ?>&nbsp; <strong>เวลา</strong> : <?php echo date("H:i:s"); ?>
<br />
<strong>โต๊ะที่</strong> : <?php echo $_SESSION["table"]; ?><br />
<strong>แคชเชียร์</strong> : <?php echo $_SESSION["user_name"]." ".$_SESSION["user_surname"]; ?>
<br />
  <br />
</div>
  <div class="headbill"><?php
	$Query = "SELECT * FROM member,member_session,membertype WHERE membertype.memtype_id = member.memtype_id AND member.member_code=member_session.member_session_code AND member_session.member_session_table='".$_SESSION["table"]."'";
	$dbquery = mysql_query($Query);
	$result = mysql_fetch_assoc($dbquery);
	if($result){
		$discount = 100-($result['memtype_discount']*100);
	echo "<b>ชื่อลูกค้า :</b> ".$result['member_name']." ".$result['member_surname']."&nbsp;&nbsp;&nbsp; <b> ส่วนลด :</b> ".$discount."%";
	}
	
	$Query1 = "SELECT member_non_session.member_non_session_name as member_non_session_name,membertype.memtype_name as memtype_name FROM member_non_session,member,membertype WHERE member_non_session.member_non_session_code = member.member_code AND member.memtype_id = membertype.memtype_id AND member_non_session.member_non_session_table ='".$_SESSION["table"]."'";
	$dbquery1 = mysql_query($Query1);
	$result1 = mysql_fetch_assoc($dbquery1);
	
	if($result1){
	echo "<b>ชื่อลูกค้า :</b> ".$result1['member_non_session_name']."&nbsp;(".$result1['memtype_name'].")";
	}
?></div></p>
    <br />
	<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="listorder">
  <thead>
  <tr>
    <td width="6%">#</td>
    <td width="45%">รายการ</td>
    <td width="12%">@</td>
    <td width="12%">Dis.</td>
    <td width="15%">รวม</td>
  </tr>
  </thead>
 <?php

//============ ทำการแสงรายการที่ได้เลือกว่ามีกี่รายการ

$strTotal=0;
$cost=0;

//============ แสดงจำนวนรายการสินค้า
	$Query = "SELECT sale_session.sale_session_id as sale_session_id,sale_session.sale_session_prod as sale_session_prod,sum(sale_session.sale_session_qty) as sale_session_qty,sale_session.sale_session_table as sale_session_table,product.prod_barcode as prod_barcode,product.prod_name as prod_name,product.prod_price as prod_price,product.prodtype_id as prodtype_id FROM product,sale_session WHERE product.prod_barcode=sale_session.sale_session_prod AND sale_session.sale_session_table='".$_SESSION["table"]."' GROUP BY sale_session.sale_session_prod ORDER BY sale_session.sale_session_id ASC";
	$dbquery = mysql_query($Query);
	
	while($result= mysql_fetch_array($dbquery))
		{
?>
  <tr>
    <td align="right" style="padding-right:5px"><?php echo $result['sale_session_qty']; ?></td>
    <td align="left" style="padding-left:5px"><?php echo substr_utf8($result['prod_name'],0,15);?></td>
    <td align="right" style="padding-right:5px"><?php echo "".number_format($result['prod_price'], 0,'.',','); ?></td>
    <td align="right" style="padding-right:5px"><?php
	$Query11 = "SELECT membertype.memtype_discount as memtype_discount  FROM member,membertype,member_session WHERE member.member_code=member_session.member_session_code AND member.memtype_id = membertype.memtype_id AND member_session.member_session_table='".$_SESSION["table"]."'";
	$dbquery11 = mysql_query($Query11);
	$result11 = mysql_fetch_assoc($dbquery11);
	
	if($result11){
	$discount = $result["prod_price"]*$result['sale_session_qty'];
	
	if($result['prodtype_id']=="3"){
		$dc = '0';
	} else {
		$dc = floor((1-$result11['memtype_discount'])*$discount);
	}
	
	echo "".number_format($dc, 0,'.',',');
	//echo $result11['memtype_discount'];
	} else {
	echo "0";
	}
?></td>
    <td align="right" style="padding-right:8px"><?php
	if($result11){
	$total2 = $discount-($dc);
	} else {
	$total2 = $result["prod_price"]*$result['sale_session_qty'];
	}
	echo "".number_format($total2, 0,'.',',');
?></td>
	  </tr>
 <?php 
		$strTotal=$strTotal+$total2;
		}
?>
<tfoot>
  <tr>
    <td colspan="4" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:8px"><?php echo "".number_format($strTotal, 0,'.',','); ?></td>
	</tr>
</tfoot>
</table>
</td>
  </tr>
</table>
</body>
</html>
